package com.zysc.processflow.common.util;

import com.alibaba.fastjson2.JSON;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.text.StringSubstitutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;

@Component
public class StringUtils {
    private static Logger logger = LoggerFactory.getLogger(StringUtils.class);
    public static String replaceVar(Map<String,Object> vars,String template){
        if(!org.springframework.util.StringUtils.hasLength(template)){
            return null;
        }
        if(MapUtils.isEmpty(vars)){
            return null;
        }
        StringSubstitutor stringSubstitutor = new StringSubstitutor(vars);
        return stringSubstitutor.replace(template);
    }


    public static Boolean equalsAndNotNull(String target , String resource){
        if(target==null || resource==null){
            return false;
        }else {
            if(target.equals(resource)){
                return true;
            }else {
                return false;
            }
        }
    }
    public static String percent(int x, int y) {
        if(y==0){
            return "0.00%";
        }
        String percent = "";
        double xx = x * 100.0;
        double yy = y * 100.0;
        double zz = xx / yy;
        if(zz<0.004 && zz>0.00){
        }
        DecimalFormat df = new DecimalFormat("##.00%");
        if(Math.abs(zz)<0.000000000001){
            percent = "0.00%";
        } else {
            percent = df.format(zz);
        }
        if(percent.startsWith(".")){
            percent = "0"+percent;
        }
        return percent;
    }
    public static boolean isNumeric(String str){
        for(int i=str.length();--i>=0;){
            int chr=str.charAt(i);
            if((chr<48 || chr>57) && chr!=46)
                return false;
        }
        return true;
    }

    public static BigDecimal tryToBigDecimal(String value){
        BigDecimal result = new BigDecimal(0);
        try{
            result = new BigDecimal(value);
            return result;
        }catch (Exception e){
            return result;
        }
    }

    public static boolean isNull(String str){
        return str == null || "".equals(str);
    }

    public static List startPage(List list, long pageNum, long pageSize) {
        if (list == null) {
            return Collections.emptyList();
        }
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        //记录总数
        Integer count = list.size();

        //开始索引
        long fromIndex = (pageNum - 1) * pageSize;
        //结束索引
        long toIndex = pageNum * pageSize;
        if (fromIndex + 1 > count) {
            return Collections.emptyList();
        }
        if (pageNum * pageSize > count) {
            toIndex = count;
        }
        return list.subList((int) fromIndex, (int) toIndex);
    }

    public static String getDistrcitName(String code){
        Map<String,String> districtMap = new HashMap() {{
            put("sys_330800", "市辖区");
            put("sys_330802", "柯城区");
            put("sys_330803", "衢江区");
            put("sys_330822", "常山县");
            put("sys_330824", "开化县");
            put("sys_330825", "龙游县");
            put("sys_330881", "江山市");
            put("sys_330851", "智造新城");
            put("330800", "市辖区");
            put("330802", "柯城区");
            put("330803", "衢江区");
            put("330822", "常山县");
            put("330824", "开化县");
            put("330825", "龙游县");
            put("330881", "江山市");
            put("330851", "智造新城");

            put("sys_330800yq", "市辖区");
            put("sys_330802yq", "柯城区园区");
            put("sys_330803yq", "衢江区园区");
            put("sys_330822yq", "常山县园区");
            put("sys_330824yq", "开化县园区");
            put("sys_330825yq", "龙游县园区");
            put("sys_330881yq", "江山市园区");
            put("sys_330851yq", "智造新城");
            put("330800yq", "市辖区");
            put("330802yq", "柯城区园区");
            put("330803yq", "衢江区园区");
            put("330822yq", "常山县园区");
            put("330824yq", "开化县园区");
            put("330825yq", "龙游县园区");
            put("330881yq", "江山市园区");
            put("330851yq", "智造新城");
        }};
        String string = districtMap.getOrDefault(code, "").toString();
        return string;
    }
}
